package org.web2024.service.serviceimp;


import org.apache.ibatis.cache.Cache;
import org.springframework.data.redis.core.RedisTemplate;



public class MybatisRedisCache implements Cache {

    private String id;



    private RedisTemplate<String,Object> redisTemplate;

    private RedisTemplate getRedisTemplate(){
        //通过ApplicationContextHolder工具类获取RedisTemplate
        if (redisTemplate == null) {
            redisTemplate = (RedisTemplate) ApplicationContextHolder.getBeanByName("redisTemplate");
        }
        return redisTemplate;
    }



    public MybatisRedisCache(String id) {
        this.id = id;
    }


    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public void putObject(Object key, Object value) {
        getRedisTemplate().opsForValue().set(key.toString(),value,60*1000L);
    }

    @Override
    public Object getObject(Object key) {
        return getRedisTemplate().opsForValue().get(key.toString());
    }

    @Override
    public Object removeObject(Object key) {
        if (key!=null)
            getRedisTemplate().delete(key.toString());
        return null;
    }

    @Override
    public void clear() {

    }

    @Override
    public int getSize() {
        return 0;
    }
}
